package leeon.bbsbrower.views;

import ipworks.IPWorksException;
import leeon.bbsbrower.actions.BBSBoardAction;
import leeon.bbsbrower.models.BoardObject;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class BoardListView extends ViewPart {
	public static final String ID = "bbsbrower.view.boardlist";

	private Table table = null;
	
	//当前讨论块的标记
	private static int block = 0;
	private static int block1 = 0;
	
	//当前块的URL
	private static String url = null;
	private static String url1 = null;
		
	//版面数据
	private static BoardObject[] boards = null;
	private static BoardObject[] boards1 = null;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		
		table = new Table(parent, SWT.READ_ONLY|SWT.BORDER|SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		
		TableColumn col1 = new TableColumn(table, SWT.NONE);
		col1.setText("序号");
		col1.setWidth(50);
		
		TableColumn col2 = new TableColumn(table, SWT.NONE);
		col2.setText("名称");
		col2.setWidth(200);
		
		TableColumn col3 = new TableColumn(table, SWT.NONE);
		col3.setText("类型");
		col3.setWidth(100);
		
		TableColumn col4 = new TableColumn(table, SWT.NONE);
		col4.setText("中文描述");
		col4.setWidth(250);
		
		TableColumn col5 = new TableColumn(table, SWT.NONE);
		col5.setText("斑竹");
		col5.setWidth(100);
		
		TableColumn col6 = new TableColumn(table, SWT.NONE);
		col6.setText("文章数");
		col6.setWidth(100);
		
		TableColumn col7 = new TableColumn(table, SWT.NONE);
		col7.setText("更新时间");
		col7.setWidth(200);
				
		//上下左右的事件
		table.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//左
				if (e.keyCode == SWT.ARROW_LEFT) {					
					//当前是目录
					if (BoardListView.url.indexOf("boa?board") != -1) {
						actionToBoardList();
					//当前在第一级列表
					} else {						
						actionToBlock();
					}					
					e.doit = false;
					
				//右
				} else if (e.keyCode == SWT.ARROW_RIGHT || e.keyCode == 13) {										
					//根据版面列表查询版面目录的action
					int i = table.getSelectionIndex();
					String type = BoardListView.boards[i].getType();
					
					//进入目录
					if ("[目录]".equals(type)) {
						actionBoardDir();
					//进入文章列表
					} else {						
						actionDocList();
					}										
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});
	}
	
	
	/**
	 * 回讨论块列表
	 */
	private void actionToBlock() {		
		//跳转回到讨论块
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			BlockView block = (BlockView)page.showView(BlockView.ID);
			block.refreshData(BoardListView.block);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 回某个BoardList
	 */
	private void actionToBoardList() {
		//跳转回到版面列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			BoardListView board = (BoardListView)page.showView(BoardListView.ID);
			board.refreshData(-1, null, BoardListView.block, 4);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 进入某个版面
	 */
	private void actionDocList() {
		//根据版面列表查询版面下文章的action
		int bd = table.getSelectionIndex();
		//目录的url
		String url = BoardListView.boards[bd].getURL();
		String bn = BoardListView.boards[bd].getName();
		
		//跳转进入某个版面
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			DocView doc = (DocView)page.showView(DocView.ID);
			doc.refreshData(bd, bn, url, 0, 0, null);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 进入某个目录
	 */
	private void actionBoardDir() {
		//根据版面列表查询版面目录的action
		int bd = table.getSelectionIndex();
		//目录的url
		String url = BoardListView.boards[bd].getURL();
		
		
		//跳转进入某个目录
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			BoardListView board = (BoardListView)page.showView(BoardListView.ID);
			board.refreshData(bd, url, 0, 2);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 刷新数据
	 * @param blocks
	 * type
	 * 1 从讨论块列表进入，             block 为进入的讨论块2 , url 为boa?2,         index为0
	 * 2 从讨论区列表的一个目录进入     block 为进入的目录块10, url 为boa?Board=xxx, index为0
	 * 3 从文章列表回到                 block 为-1            , url 为null,          index为11
	 * 4 从讨论区列表回到上一级目录     block 为-1            , url 为null,          index为12
	 */
	public void refreshData(int block, String url, int index, int type) {
		//提取数据
		if (type == 1 || type == 2) {									
			//这是一个目录下的版面列表，现在是从上级版面列表进入列表返回该表
			if (type == 2) {
				BoardListView.boards1 = BoardListView.boards;
				BoardListView.url1 = BoardListView.url;
				BoardListView.block1 = BoardListView.block;
			//从blocks列表进入
			} else {
				BoardListView.boards1 = null;
				BoardListView.boards = null;
				BoardListView.url1 = null;
				BoardListView.url = null;
				BoardListView.block1 = -1; 
				BoardListView.block = -1;
			}
			
			BoardListView.url = url;
			BoardListView.block = block;
			try {
				BoardListView.boards = BBSBoardAction.getBoardsOfBlockByURL(url);
			} catch (IPWorksException e) {
				MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());
				e.printStackTrace();
			}
						
		} else {
			//从文章列表返回该表
			if (type == 3) {
				//这是一个一级的boardlist
				if (BoardListView.boards1 == null) {
					
				//这是一个二级的boardlist
				} else {
					
				}				
			//从下级列表返回
			} else {
				BoardListView.boards = BoardListView.boards1;
				BoardListView.url = BoardListView.url1;
				BoardListView.block = BoardListView.block1;
			}
		}
						
		table.removeAll();
		if (BoardListView.boards != null) {
			for (int i = 0; i < BoardListView.boards.length; i ++) {
				TableItem it = new TableItem(table, SWT.NONE);
				it.setText(new String[] {
						BoardListView.boards[i].getNumber(), 
						BoardListView.boards[i].getName(),
						BoardListView.boards[i].getType(), 
						BoardListView.boards[i].getCh(), 
						BoardListView.boards[i].getMaster(), 
						BoardListView.boards[i].getDocNumber(), 
						BoardListView.boards[i].getUpdateTime()}
				);
				it.setData(BoardListView.boards[i]);
			}
			//选中某个
			table.setSelection(index);
		}
		ApplicationActionBarAdvisor.setStatusLine();
	}	
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		table.setFocus();
	}
}